package firstGrabber;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
 
public class TwitterGrabber 
{
    final String[] KEYWORDS = {"."};
    
    public static void main(String[] args) throws IOException 
    {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	
    	// Enter search term
    	System.out.print("Enter searchword: ");
        String search = br.readLine();
        
        // Execute search with users term
        new TwitterGrabber().grabber(search);
    }
 
 
    private void grabber(String s) 
    {
        BufferedReader br = null;
        InputStreamReader isr = null;
        URL url = null;
        
        // Building the search-URL for twitter
        // Result_type = recent, gives only the latest tweets
        // rpp = 100, shows 100 tweets on one page so we get the latest 100
        try 
        {
            url = new URL("http://search.twitter.com/search.json?q="+s+"&result_type=recent&rpp=100");
        } 
        catch (MalformedURLException e) 
        {
            e.printStackTrace();
        }
 
        if(url != null) 
        {
            try 
            {
                isr = new InputStreamReader(url.openStream());
            } 
            catch (IOException e1) 
            {
                e1.printStackTrace();
            }
            
            // Creating a temp.txt for the search result
            // Just a first try to see how the output can be changed
            // And to have a better overview of the result itself
            br = new BufferedReader(isr);
            String line = null;
            File file = new File("temp.txt");
            FileOutputStream output = null;
            
            try 
            {
            	output = new FileOutputStream(file);
            } 
            catch (FileNotFoundException e1) 
            {
                e1.printStackTrace();
            }
            
            String[] list = new String[100000];
            DataOutputStream out = new DataOutputStream(output);
            
            try 
            {
                while((line = br.readLine()) != null) 
                {
                	try 
                    {
                        out.writeBytes(line+"\n");
                    }
                    catch (IOException e) 
                    {
                    }
                	
                	// Adding all information from the tweets to the String-Array
                	list = line.split(",");
                }
 
            } 
            catch (IOException e2) 
            {
                e2.printStackTrace();
            }
            
            // Output of the core information
            // User and tweet-text at the moment
            // Geo-Location and stuff is available as well
            for(int i = 0; i < list.length; i++)
            {
            	if(list[i].contains((char)34+"from_user"+(char)34) )
            	{
            		System.out.println(list[i]);
            	}
            	if(list[i].contains((char)34+"text"+(char)34))
            	{
            		System.out.println(list[i]);
            	}
            }
        }
    }
}